package fr.umlv.ig.MetroView;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import fr.umlv.ig.layout.JLayeredPaneLayout;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.xml.MetroModelFactory;

@SuppressWarnings("serial")
public class MetroMap extends JLayeredPane {

	public MetroMap(MetroModelImpl metroModel, BufferedImage image) {
		setLayout(new JLayeredPaneLayout());
		
		final MetroMapTrains metroMapTrains = new MetroMapTrains(metroModel);
		metroMapTrains.generateTrain();
		metroMapTrains.initJTrains();
		metroMapTrains.setOpaque(true);
		metroMapTrains.setBackground(Color.GREEN);
		
//		Train train = new Train(1,"train1");
//		Point p = new Point(100, 300);
//		train.setCoordinate(p);
//		System.out.println(train.getCoordinate());
//		final JTrain jtrain = new JTrain(train);
//		jtrain.setBounds(100, 300, 10, 10);
//		jtrain.drawTrain();
//		metroMapTrains.add(jtrain);
//		repaint();
//		revalidate();
		
		
		metroMapTrains.setPreferredSize(new Dimension(800,550));
		metroMapTrains.setVisible(true);
		metroMapTrains.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("metroMapTrains x : " + e.getX());
				metroMapTrains.timer.start();
				//jtrain.setLocation(jtrain.getX()+5, jtrain.getY()+5);
//				System.out.println("trainpostion : " + jtrain.getPoint());
//				jtrain.move();
			}
			
		});
		add(metroMapTrains,Integer.valueOf(2));
		

		MetroMapStations metroMapStations = new MetroMapStations(metroModel, image);
		metroMapStations.drawMap();
		metroMapStations.setOpaque(true);
		metroMapStations.setPreferredSize(new Dimension(900,650));
		metroMapStations.setVisible(true);
		add(metroMapStations,Integer.valueOf(1));
	}
	
	public static void main(String[] args) {
		JFrame f=new JFrame("MetroMap Test");
		JPanel panel = new JPanel();
		f.setPreferredSize(new Dimension(1000, 700));
		MetroModelFactory metroFactory = null;
		try {
			metroFactory = MetroModelFactory.newInstance();
		} catch (ParserConfigurationException e) {
			System.err.println(e.getMessage());
			return;
		} catch (SAXException e) {
			System.err.println(e.getMessage());
			return;
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return;
		}

		BufferedImage imageMap = new BufferedImage(900, 650, BufferedImage.TYPE_3BYTE_BGR);
		MetroModelImpl model = metroFactory.getMetroModelImpl();
		MetroMap metroMap = new MetroMap(model, imageMap);
		metroMap.setVisible(true);
		panel.add(metroMap);
		f.getContentPane().add(panel);
		f.pack();
		f.setDefaultCloseOperation(args.length == 0 ? JFrame.EXIT_ON_CLOSE : WindowConstants.DISPOSE_ON_CLOSE);
		f.setVisible(true);
	}
}
